/**
 * Created by liujiaxing on 2016/12/30.
 */
import Vue from 'vue';
import tpl from './tpl.html';
import commonService from '../../common/commonService';
import Loading from '../Loading.vue';
import filter from '../../util/filter';

export default Vue.extend({
    name: "total",
    template: tpl,
    components: {
        Loading
    },
    data(){
        return {
            options: {
                len: 8
            },
            arr: ['0', '0', '0', '0', '0', '0', '0', '0'],
            arr1: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            totalnum: 0,
            isLoading: false,
            title:window.app.abroad===true?'total':'累积果仁成交量'
        }
    },
    mounted(){
        this.init();
    },
    methods: {
        init(){
            this.run();
        },
        zfill(num, size){
            let s = "000000000" + num;
            return s.substr(s.length - size);
        },
        getNewArr(oldNum,newNum){
            let newArr=[];
            newArr.push(oldNum);
            newArr.push(newNum);
            return newArr;
        },
        run(){
            let that = this;
            this.isLoading = true;
            commonService.onLoopTradeInfo((rep) => {
                if(this.isLoading == true){
                    this.isLoading = false;
                    setTimeout(()=>{
                        let digitSets=$(".digit_set");
                        $.each(digitSets,function (index,digitSet) {
                            $(digitSet).find(".digit").eq(9).addClass('previous');
                            $(digitSet).find(".digit").eq(0).addClass('active');
                        });
                    },10);
                }
                else this.isLoading = false;
                setTimeout(function () {
                    let oldnum = that.totalnum;
                    let newnum = filter.floorFix(rep.total,0);
                    let oldlength = oldnum.toString().length;
                    let newlength = newnum.toString().length;
                    that.totalnum = newnum;
                    let oldNums = that.zfill(oldnum, that.options.len).toString().split("");
                    let newNums = that.zfill(newnum, that.options.len).toString().split("");
                    let digitSet=$(".digit_set");
                    $.each(newNums, function (index, val) {
                        if (that.options.len - index > newlength) {
                            digitSet.eq(index).hide();
                        }
                        if (that.options.len - index == newlength) {
                            digitSet.eq(index).show();
                        }
                        if (that.options.len - index <= newlength) {
                            let el = $(digitSet.eq(index));
                            el.show();
                            if (that.options.len - index <= oldlength) {
                                if (oldNums[index] !== val) {
                                    let current = el.find('.active'),
                                        previous = el.find('.previous');
                                    previous.removeClass('previous');
                                    current.removeClass('active').addClass('previous');
                                    setTimeout(()=>{
                                        el.find('.digit').eq(val).addClass('active')
                                    }, 10);
                                }
                            }
                            else {
                                if(val!=='0') {
                                    let current = el.find('.active'),
                                        previous = el.find('.previous');
                                    previous.removeClass('previous');
                                    current.removeClass('active').addClass('previous');
                                    setTimeout(()=> {
                                        el.find('.digit').eq(val).addClass('active')
                                    }, 10);
                                }
                            }
                        }
                    });
                    setTimeout(()=>{
                        if(oldlength!==newlength){
                            for(let i=that.options.len-4;i>=0;i-=3){
                                digitSet.eq(i).addClass('longMargin');
                            }
                        }
                    },0);
                },10);
            });
        }

    }
});
